home *** CD-ROM | disk | FTP | other *** search
/ MacFormat 1997 October / macformat-055.iso / mac / Shareware Plus / Developers / Caveman Sound System / sndtest.c < prev    next >
Encoding:
C/C++ Source or Header  |  1997-06-20  |  6.6 KB  |  204 lines  |  [TEXT/ALFA]

  1. /*****************************************************************************
  2.  * FILE:        sndtest.c
  3.  * AUTHOR:        David Hay
  4.  * CREATED:        March 9, 1995
  5.  * DESCRIPTION:    Test program and tutorial for using the Caveman Sound System.
  6.  *
  7.  * Copyright © 1995-1997 David Hay
  8.  *
  9.  * Permission to use, copy, and distribute this software and its documentation
  10.  * for any purpose is hereby granted without fee,  provided that (i) the above
  11.  * copyright notices  and  this permission notice  appear in all copies of the
  12.  * software  and  related documentation,  and (ii) the names of David Hay  and
  13.  * Caveman Creations may not be used in any advertising or publicity  relating
  14.  * to the software without the specific, prior written permission of David Hay
  15.  *
  16.  * THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND, EXPRESS,
  17.  * IMPLIED  OR  OTHERWISE,  INCLUDING,  WITHOUT  LIMITATION,  ANY WARRANTY  OF
  18.  * MERCHANTABILITY  OR  FITNESS FOR  A PARTICULAR PURPOSE.  IN NO EVENT  SHALL
  19.  * DAVID HAY  OR  CAVEMAN CREATIONS  BE LIABLE  FOR  ANY SPECIAL,  INCIDENTAL,
  20.  * INDIRECT  OR  CONSEQUENTIAL DAMAGES OF ANY KIND,  OR ANY DAMAGES WHATSOEVER
  21.  * RESULTING FROM LOSS OF USE, DATA OR PROFITS,  WHETHER OR NOT ADVISED OF THE
  22.  * POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF LIABILITY, ARISING OUT OF OR IN
  23.  * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  24.  *****************************************************************************/
  25.  
  26. #ifndef __CMSOUNDSYSTEM__
  27. #include "CMSoundSystem.h"
  28. #endif
  29.  
  30.  
  31. #define kNumChannels    1        /* The number of sound channels to open  */
  32. #define kSoundToPlay    31000    /* Resource ID of the sound to play  */
  33. #define kSoundToPlay2    128        /* Resource ID of another sound to play */
  34.  
  35.  
  36. /*  Standard Mac Toolbox initialization  */
  37. static void
  38. InitMacintosh( void )
  39. {
  40.     MaxApplZone();
  41.     
  42.     InitGraf( &qd.thePort );
  43.     InitFonts();
  44.     FlushEvents( everyEvent, 0 );
  45.     InitWindows();
  46.     InitMenus();
  47.     TEInit();
  48.     InitDialogs( 0L );
  49.     InitCursor();
  50. }
  51.  
  52. /*---------------------------------------------------------------------------*/
  53.  
  54. void main( void )
  55. {
  56.     short    ii;            /* misc counter */
  57.     short    sndRef;        /* sound reference number for sound 1 */
  58.     short    sndRef2;    /* sound reference number for sound 2 */
  59.     long    ignored;
  60.     OSErr    err;
  61.     
  62.     InitMacintosh();
  63.     
  64.         /*  First you must initialize the sound system by specifying the
  65.         **  number of channels you wish to use.
  66.         **
  67.         **  • 1.0 - Initialization of the sound tool has been simplified since
  68.         **            version 1.0.  Previously one had to call CMSInitSound() and
  69.         **            CMSOpenAllChannels().
  70.         */
  71.     err = CMSOpenSoundTool( kNumChannels );
  72.     
  73.         
  74.         /*    In order to play a sound, we first need to load it in.  You give
  75.         **  a resource number and it loads that sound resource and registeres
  76.         **  it with the sound system.  A reference number is returned in the
  77.         **    second parameter.
  78.         */
  79.     if ( err == noErr )
  80.         err = CMSLoadSound( kSoundToPlay, &sndRef );
  81.         
  82.     if ( err == noErr )
  83.         err = CMSLoadSound( kSoundToPlay2, &sndRef2 );
  84.  
  85.  
  86.         /*    Let's see what happens when we remove a sound and the load it
  87.         **    back in.  Normally you wouldn't do this right after you loaded
  88.         **    it, but this is as good a place as any to show how it's done.
  89.         */
  90.     if ( err == noErr )
  91.         err = CMSRemoveSound( sndRef );
  92.  
  93.     if ( err == noErr )
  94.         err = CMSLoadSound( kSoundToPlay, &sndRef );
  95.     
  96.  
  97.         /*  Now that we've loaded a sound, play it using the reference number
  98.         **  we got from CMSLoadSound() or CMSRegisterSound(), depending on
  99.         **  how the sound was registered.  We'll use sound channel 0.
  100.         */
  101.     if ( err == noErr )
  102.         err = CMSPlaySound( sndRef, 0 );
  103.     
  104.         /*  Let's play with the volume.  Wait 2 seconds, then drop the
  105.         **    sound to 50%, wait another 2 seconds then back to full volume.
  106.         **
  107.         **    • 1.1 - CMSSetChannelVolume() is a new function.
  108.         */
  109.     if ( err == noErr )
  110.     {
  111.         Delay( 2L * 60L, &ignored );
  112.         err = CMSSetChannelVolume( 0, 0 );
  113.     }
  114.     
  115.     if ( err == noErr )
  116.     {
  117.         Delay( 2L * 60L, &ignored );
  118.         err = CMSSetChannelVolume( 0, 256 );
  119.     }
  120.     
  121.         /*  Since we played the last sound asynchronously, we want to
  122.         **  find out when it stops so we can do other stuff.  To do
  123.         **  this we use the CMSGetSoundPlaying() routine.  This returns
  124.         **  the sound reference number of the currently playing sound,
  125.         **  kSoundResource or kNoSound in the sndRef paramater.  I
  126.         **  could have used the routine CMSWaitForSilence() here, but
  127.         **  very often the sound was played asynchronously so you could
  128.         **  do other stuff while the sound is playing.  Plus I wanted to
  129.         **  show how the CMSGetSoundPlaying() function worked.  ;-)
  130.         */
  131.     while ( err == noErr )
  132.     {
  133.         err = CMSGetSoundPlaying( 0, &sndRef );
  134.         if ( sndRef == kNoSound )
  135.             break;
  136.     }
  137.     
  138.     
  139.         /*  Now let's play some music.  First call CMSLoadMusic() to
  140.         **  load the music blocks into memory and register them with
  141.         **  the sound system.
  142.         */
  143.     if ( err == noErr )
  144.         err = CMSLoadMusic( 128 );
  145.     
  146.     if ( err == noErr )
  147.     {
  148.             /*  Start the music going with CMSPlayMusic().  Here we indicate
  149.             **  that the music should be played on sound channel 0.  This
  150.             **  will play the music asynchronously.  If a loop has been
  151.             **  defined in the 'MUSL' resource, then the music will loop
  152.             **  until it is stopped with a call to CMSStopSound() or another
  153.             **  sound is played on the same channel.  My sound sample does
  154.             **  have a loop, so it will just keep going until we interrupt it
  155.             */
  156.         err = CMSPlayMusic( 0 );
  157.     }
  158.     
  159.         /*    Lets try out the sound priority mechanism and interrupt the music
  160.         **    with another sound.  By default, every sound has a priority of 0,
  161.         **  but we can override it with a higher priority.  Here we'll use 5.
  162.         **
  163.         **    • 1.1 - CMSPlaySoundPriority() is a new function.
  164.         */
  165.     if ( err == noErr )
  166.     {
  167.         Delay( 4L * 60L, &ignored );
  168.         err = CMSPlaySoundPriority( sndRef2, 5 );
  169.     }
  170.  
  171.         /*  Wait for that last sound to finish playing.  The check on err
  172.         **  is a result of the return value from CMSPlaySoundPriority() which
  173.         **  will return kPriorityOverride when a sound was overridden by the
  174.         **  new sound. (i.e. we were able to start the sound)
  175.         */
  176.     if ( err == noErr || err == kPriorityOverride )
  177.         err = CMSWaitForSilence( 0 );
  178.     
  179.     
  180.         /*  Now start the music again, wait for a while, then fade out  */
  181.         
  182.     if ( err == noErr )
  183.         err = CMSPlayMusic( 0 );
  184.         
  185.     if ( err == noErr )
  186.     {
  187.         Delay( 10L * 60L, &ignored );
  188.         
  189.         for ( ii = 256; ii >= 0 && err == noErr; ii -= 8 )
  190.         {
  191.             err = CMSSetChannelVolume( 0, ii );
  192.             Delay( 10L, &ignored );
  193.         }
  194.     }
  195.  
  196.         /*  We're finished with the sound system, so call CMSDisposeSound()
  197.         **  to close and free the sound channels and any sounds registered
  198.         **  with the sound system.
  199.         */
  200.     CMSDisposeSound();
  201.     
  202.     FlushEvents( everyEvent, 0 );
  203.     if ( err != noErr )    SysBeep( 10 );
  204. }